package io.renren.modules.app.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.app.annotation.Login;
import io.renren.modules.app.entity.VideoCategoryEntity;
import io.renren.modules.app.service.VideoCategoryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping({"app/videocategory"})
public class VideoCategoryController
{

  @Autowired
  private VideoCategoryService videoCategoryService;

  @RequestMapping({"/list"})
  @Login
  public R list(@RequestParam Map<String, Object> params)
  {
    PageUtils page = this.videoCategoryService.queryPage(params);

    return R.ok().put("page", page);
  }

  @RequestMapping({"/listAll"})
  public R listAll(@RequestParam Map<String, Object> params)
  {
    List list = this.videoCategoryService.list();
    PageUtils page = new PageUtils(list, list.size(), list.size(), 1);
    return R.ok().put("page", page);
  }

  @RequestMapping({"/info/{categoryId}"})
  @RequiresPermissions({"app:videocategory:info"})
  public R info(@PathVariable("categoryId") Integer categoryId)
  {
    VideoCategoryEntity videoCategory = (VideoCategoryEntity)this.videoCategoryService.getById(categoryId);

    return R.ok().put("videoCategory", videoCategory);
  }

  @RequestMapping({"/save"})
  @RequiresPermissions({"app:videocategory:save"})
  public R save(@RequestBody VideoCategoryEntity videoCategory)
  {
    this.videoCategoryService.save(videoCategory);

    return R.ok();
  }

  @RequestMapping({"/update"})
  @RequiresPermissions({"app:videocategory:update"})
  public R update(@RequestBody VideoCategoryEntity videoCategory)
  {
    this.videoCategoryService.updateById(videoCategory);

    return R.ok();
  }

  @RequestMapping({"/delete"})
  @RequiresPermissions({"app:videocategory:delete"})
  public R delete(@RequestBody Integer[] categoryIds)
  {
    this.videoCategoryService.removeByIds(Arrays.asList(categoryIds));

    return R.ok();
  }
}